package com.csthink.web.infrastructure.security.authentication.parser;

import com.csthink.web.infrastructure.security.AuthenticationHelper;
import com.csthink.web.infrastructure.security.authentication.exception.AuthenticationParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;

import javax.servlet.http.HttpServletRequest;

/**
 * Spring Security URL 模式解析基类
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-12
 */
public abstract class AbstractAuthenticationParser implements AuthenticationParser {
    //protected AuthenticationService authenticationService;

    /**
     * 不同的实现类构造 Authentication 方法不同
     *
     * @param request
     * @return
     */
    protected abstract Authentication getAuthentication(HttpServletRequest request);

    @Override
    public Authentication parseAuthentication(HttpServletRequest request) throws AuthenticationParseException {
        try {
            return getAuthentication(request);
        } catch (Exception e) {
            throw new AuthenticationParseException(e.getMessage(), e);
        }
    }

    /**
     * 不同实现类匹配对应模式的URL
     *
     * @param request
     * @return
     */
    @Override
    public boolean match(HttpServletRequest request) {
        return AuthenticationHelper.matches(request, getTargetUrlMatchers());
    }

    //@Autowired
    //public void setAuthenticationService(AuthenticationService authenticationService) {
    //    this.authenticationService = authenticationService;
    //}
}
